﻿using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Service.Entity.Helper;
using System.Collections.Generic;
using System.Linq;
using RiderShop.Mobile.Common.Component;
using Kevin.Shop.Business.Comments.Components;
using RiderShop.Mobile.ServiceEntity;

namespace RiderShop.Mobile.Modle
{
    public class ContentModel
    {
        /// <summary>
        /// 文章列表集合
        /// </summary>
        public List<ArticleInfo> ArticleList { get; set; }

        public List<CategoryAndArticleInfoExtend> CategoriesAndArticleList { get; set; }

        #region 得到文章的每个分类和每个分类的标题
        /// <summary>
        /// 得到文章的每个分类和每个分类的标题
        /// </summary>
        /// <returns></returns>
        public List<CategoryAndArticleInfoExtend> GetCategoriesAndArticleInfo()
        {
            CategoryAndArticleQuery query = new CategoryAndArticleQuery();
            query.ContentType = 0;
            var result = RestfulHelper.Query<CategoryAndArticleInfoExtend>(SiteUrl.FormatUrl("Service_SystemTools_CategoryAndArticleByContentType"), query);

            List<CategoryAndArticleInfoExtend> categoriesAndArticlesNoDistinct = null;//去重的集合为了显示分类
            List<CategoryAndArticleInfoExtend> categoriesAndArticlesDistinct = null;
            if (result != null)
            {
                if (result.Data.Count > 0)
                {
                    var distinctList = result.Data.Distinct(new ContentCategoryComparer());
                    categoriesAndArticlesDistinct = distinctList.ToList();
                    categoriesAndArticlesNoDistinct = result.Data;

                    for (int i = 0; i < categoriesAndArticlesDistinct.Count; i++)
                    {
                        categoriesAndArticlesDistinct[i].ArticleList = categoriesAndArticlesNoDistinct.Where(where => where.ContentCategoryId == categoriesAndArticlesDistinct[i].ContentCategoryId).ToList();
                    }
                    CategoriesAndArticleList = categoriesAndArticlesDistinct;
                }
            }
            return categoriesAndArticlesDistinct;
        }
        #endregion

        #region 得到具体的文章内容
        /// <summary>
        ///  得到具体的文章内容
        /// </summary>
        /// <param name="articleId">文章编号</param>
        /// <returns></returns>
        public List<ArticleInfo> GetArticleInfo(int articleId)
        {
            var afficleList = RestfulHelper.Query<ArticleInfo>(SiteUrl.FormatUrl("Service_SystemTools_Affiche", articleId));
            if (afficleList != null)
            {
                if (afficleList.Count > 0)
                {
                    return afficleList;
                }
            }
            return afficleList;
        }
        #endregion

        #region 通过文章的分类编号得到该分类的文章列表
        /// <summary>
        ///  通过文章的分类编号得到该分类的文章列表
        /// </summary>
        /// <param name="contentCategoryId">文章分类编号</param>
        /// <returns></returns>
        public void GetArticleInfoByContentCategoryId(int contentCategoryId)
        {
            var query = new ArticleCategoryQuery()
            {
                ContentCategoryId = contentCategoryId,
                PageIndex = 0,
                PageSize = 10000,
                SortBy = "ArticleId",
                SortOrder = SortAction.ASC
            };
            var result = RestfulHelper.Query<ArticleInfo>(SiteUrl.FormatUrl("SystemTools_GetArticlesByContentCategoryId"), query);
            if (result != null && result.Data.Count > 0)
            {
                ArticleList = result.Data;
            }
        }
        #endregion
    }

    public class ContentCategoryComparer : IEqualityComparer<CategoryAndArticleInfoExtend>
    {
        public bool Equals(CategoryAndArticleInfoExtend x, CategoryAndArticleInfoExtend y)
        {
            return x.ContentCategoryId.Equals(y.ContentCategoryId);
        }

        public int GetHashCode(CategoryAndArticleInfoExtend obj)
        {
            return 0;
        }
    }

}
